package ru.ipccenter.main;

import java.net.MalformedURLException;
import java.util.ArrayList;
import jcifs.smb.SmbException;
import ru.ipccenter.browser.SMBBrowser;

public class MainClass {
    
    private SMBBrowser browser = null;
    
    public MainClass(){
    } 
    
    public void newConnection(String serverName) throws Exception{
        try {
            browser = new SMBBrowser(serverName);
        } catch (Exception ex) {
            throw ex;
        }
    }
    
    public ArrayList<String> listDir() throws Exception{
        if(browser==null){
            throw new RuntimeException("No connection established yet!");
        }
        if(browser.isFile()) {
            throw new RuntimeException("Not a directory!");
        }
        ArrayList<String> retValue = null;
        try {
            retValue = browser.ls();
        } catch (NullPointerException ex) {
            throw new Exception("Error");
        } catch (SmbException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        }
        return retValue;
    }
    
    public ArrayList<String> find(String regexp, String path) throws MalformedURLException, SmbException {
        ArrayList<String> retVal = null;
        if (path.charAt(path.length()-1) == '/') {
            path = path.substring(0,path.length()-1);
        }
        retVal = browser.search(regexp, path);
        if (retVal.isEmpty()){
            retVal.add("Nothing found");
        }
        return retVal;
    }
    
    public void close(){
        browser.close();
    }
}
